import request from 'axios'
import Cookie from 'js-cookie'
import router from '@/router'; 

let DOMAIN='http://47.100.110.232:9099'
if (process.env.NODE_ENV === 'production') {
    DOMAIN='http://47.100.110.232:9099'
  } else if (process.env.NODE_ENV === 'development') {
    // DOMAIN = '/api'
    DOMAIN='http://47.100.110.232:9099'
  }
const service = request.create({
    baseURL: DOMAIN,
    headers: {
        'X-Requested-With': 'XMLHttpRequest',
    }
});

// 请求拦截器
service.interceptors.request.use(config => {
  // 是否为当前的请求加上请求头 token
  // if (localStorage.get('token')) {
  //   config.headers['x-auth-token'] = localStorage.get('token')
  // }
  if (localStorage.getItem('platformId')) {
    config.headers['platformId'] = localStorage.getItem('platformId')
  }
  if (localStorage.getItem('token')) {
    config.headers['token'] = localStorage.getItem('token')
  }
  return config
}, error => {
  return Promise.reject(error)
})
// 响应拦截器
service.interceptors.response.use(
  (response) => {
    switch (response.data.code) {
      case 401:
        router.replace('/')
        break
      default:
    }
    return response
  },
   error => {
    console.log('err' + error);
    return Promise.reject(error)
  }
)
export default service
